<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>父子组件通信实例</title>
</head>
<body>
    <div id="app"></div>
</body>
</html>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script type="text/babel">

    //增加操作组件
    class Add extends React.Component{
        constructor(props){
            super(props);
            this.state={
                addTxt:''
            };
            
            //注释了这一行，直接在onChange内bind
            //this.changeTxt=this.changeTxt.bind(this)
            
        }
        
        changeTxt(e){
            this.setState({
                addTxt:e.target.value
            })
        }

        addClick(){
            // 在这里定义onAdd事件触发
            this.props.onAdd(this.state.addTxt);
        }

        render(){
            return(
                <div>
                    <input type="text" value={this.state.addTxt} onChange={this.changeTxt.bind(this)} />
                    <button onClick={this.addClick.bind(this)}>添加</button>
                    
                </div>
            )
        }
    }

    class Item extends React.Component{
        constructor(props){
            super(props)
            console.log(props);
            this.state={
                index:props.index,
                txt:props.txt
            }
        }

        delClick(){
            this.props.onDel(this.state.index);
        }

        render(){
            return(
                <div>{this.state.txt}<button onClick={this.delClick.bind(this)}>删除{this.state.index}</button></div>
            )
        }
    }
        
    class App extends React.Component{
        constructor(props){
            super(props);
            this.state={
                items:[]
            }
        };

        addItem(item){
            this.setState({
                items:[...this.state.items,item]
            })
        }

        delItem(index){
            
            this.setState({
                items:[...this.state.items.slice(0,index),...this.state.items.slice(index+1)]
            })
        }

        render(){
            console.log('big render',this.state.items)
            let lis=this.state.items.map((item,index)=>{
                // 注意这里的key绝对不能为index，因为index会变化
                return <Item txt={item} key={item+index} index={index} onDel={this.delItem.bind(this)}/>
            })
            //onAdd触发后，父组件用什么函数接收
            return (
                <div>
                <Add onAdd={this.addItem.bind(this)}/>
                    {lis}
                </div>
            )
        }
    }
    
    ReactDOM.render(
       <App />,
        document.getElementById('app')
    )
</script>